package com.topisv.tms.web.lightShelf;

import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.Servlets;

import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.User;
import com.topisv.tms.entity.lightShelf.LightShelf;
import com.topisv.tms.entity.lightShelf.LightShelfStock;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.account.AccountService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.lightShelf.LightShelfService;
import com.topisv.tms.service.lightShelf.LightShelfStockService;
import com.topisv.tms.utils.JsonUtil;
import com.topisv.tms.web.base.BaseDataTableController;

/**
 * 灯光架_control
 * @author ch
 */
@Controller
@RequestMapping({ "/lightShelf" })
public class LightShelfController extends BaseDataTableController{

	private static final Logger logger = LoggerFactory.getLogger(LightShelfController.class);

	@Autowired
	private LightShelfService lightShelfService;
	@Autowired
	private CustomersService customersService;
	@Autowired
	private LightShelfStockService lightShelfStockService;
	@Autowired
	private AccountService accountService;
	
	
	
	/**
	 * 导向灯光架管理页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "v2_toLightShelfMgr")
	public String toLightShelfMgr(ServletRequest request,HttpServletResponse response,Model model){
		
		//查找商户组
		ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		User user=this.accountService.findUserByJobuuid(curUser.jobuuid);
		List<Customers> customerList;
		if(user.getUserType().equals("paiu")){ //派友用户
		    customerList=this.customersService.findAll();
			model.addAttribute("customerList",customerList);
		}else if(user.getUserType().equals("customer")){ //商家用户
			customerList=new ArrayList<Customers>();
			String customerCode=curUser.customerCode;
		    Customers customer=this.customersService.findByCustomerCode(customerCode);
			customerList.add(customer);
			model.addAttribute("customerList",customerList);
			model.addAttribute("customer", customer); 
		};
	    
		return "lightShelf/lightShelfMgr";
	}
	
	//查询灯光架
	@RequestMapping(value = "v2_queryLightShelf")
	public String queryLightShelf(Model model, ServletRequest request) {
		try {
			//查找商户组
			Map<String, Object> param = Servlets.getParametersStartingWith(request, "_");
			ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			User user=this.accountService.findUserByJobuuid(curUser.jobuuid);
			fillSelectValue(model, param.get("customerCode").toString());
			List<Customers> customerList;
			if(user.getUserType().equals("paiu")){ //派友用户
			    customerList=this.customersService.findAll();
				model.addAttribute("customerList",customerList);
			}else if(user.getUserType().equals("customer")){ //商家用户
				customerList=new ArrayList<Customers>();
				String customerCode=curUser.customerCode;
			    Customers customer=this.customersService.findByCustomerCode(customerCode);
				customerList.add(customer);
				model.addAttribute("customerList",customerList);
			}
			
			List<LightShelf> lightShelfList = this.lightShelfService.loadLightShelfByParam(param);
			model.addAttribute("lightShelfList", lightShelfList);
			model.addAttribute("param", Servlets.encodeParameterStringWithPrefix(param, "_"));
			  
		} catch (Exception e) {
			
			logger.error("error",e);
		}
		return "lightShelf/lightShelfMgr";
	}
	
	/**
	 * 创建灯光架
	 * @param bill
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "ajaxCreateLightShelf")
	@ResponseBody
	public String ajaxCreateLightShelf(HttpServletResponse response,ServletRequest request) {
		try {
			
			String shelfName=request.getParameter("shelfName");
			String customerCode=request.getParameter("customerCode");
			String warehouseCode=request.getParameter("warehouseCode");
			String shelfType=request.getParameter("shelfType");
			String boardCode=request.getParameter("boardCode");
			
			
			LightShelf lightShelf=new LightShelf();
			
			Random r=new Random();
			DecimalFormat df=new DecimalFormat("000");
			String shelfCode=null;
			while(true){
				String temp=df.format(r.nextInt(999));
				shelfCode="L"+warehouseCode+temp;
				LightShelf tempShelf=this.lightShelfService.loadLightShelfByShelfCode(shelfCode);
				if(tempShelf==null){
					break;
				}
			}
		    lightShelf.setShelfCode(shelfCode);
			lightShelf.setShelfName(shelfName);
			lightShelf.setCustomerCode(customerCode);
			lightShelf.setWarehouseCode(warehouseCode);
			lightShelf.setShelfType(shelfType);
			lightShelf.setBoardInfo(boardCode);
			if("1".equals(shelfType))
			{
				lightShelf.setLineNum(5);
				lightShelf.setRowNum(18);
				lightShelf.setStockNum(90);
			}
			else if("2".equals(shelfType))
			{
				lightShelf.setLineNum(10);
				lightShelf.setRowNum(18);
				lightShelf.setStockNum(180);
			}
			else
			{
				lightShelf.setLineNum(5);
				lightShelf.setRowNum(18);
				lightShelf.setStockNum(90);
			}
			
			this.lightShelfService.save(lightShelf);
			
			return "true";
			
		} catch (Exception e) {
			
			logger.error("error",e);
			return "false";
		}
	}
	
	/**
	 * 删除灯光架
	 * @param lightShelfId 灯光架ID
	 * @return
	 */
	@RequestMapping(value = "ajaxDeleteLightShelf")
	@ResponseBody
	public String ajaxDeleteLightShelf(HttpServletResponse response,ServletRequest request) {
		try {
			Long lightShelfId=Long.valueOf(request.getParameter("lightShelfId"));
			this.lightShelfService.delete(lightShelfId);
			return "true";
			
		} catch (Exception e) {
			
			logger.error("error",e);
			return "false";
		}
	}
	
	/**
	 * 释放灯光架
	 * @param lightShelfId 灯光架ID
	 * @return
	 */
	@RequestMapping(value = "ajaxFree")
	@ResponseBody
	public String ajaxFree(HttpServletResponse response,ServletRequest request) {
		try {
			Long lightShelfId=Long.valueOf(request.getParameter("lightShelfId"));
			this.lightShelfService.free(lightShelfId);
			return "true";
			
		} catch (Exception e) {
			
			logger.error("error",e);
			return "false";
		}
	}
	
	/**
	 * 查询灯光架详细信息
	 * @param lightShelfId 灯光架ID
	 * @return
	 */
	@RequestMapping(value = "ajaxLoadLightShelfDetail")
	public void ajaxLoadLightShelfDetail(HttpServletResponse response,ServletRequest request) {
		try {
			Long lightShelfId=Long.valueOf(request.getParameter("lightShelfId"));
			LightShelf lightShelf=this.lightShelfService.loadLightShelfById(lightShelfId);
			List<LightShelfStock> stockList=this.lightShelfStockService.loadStock(lightShelf.getId());
			
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("lightShelf", lightShelf);
			map.put("stockList", stockList);
			String json = JsonUtil.getObjectString(map);
			PrintWriter out;
			out = response.getWriter();
			out.print(json);
			
			
		} catch (Exception e) {
			
			logger.error("error",e);
		}
	}
	
}


